//
//  ActivityDetailController.m
//  XiangCaoJiaYuan
//
//  Created by long Kan on 17/10/9.
//  Copyright © 2017年 tongj. All rights reserved.
//

#import "ActivityDetailController.h"
#import "ActivityDetailCell.h"
#import "ActivityDetailModel.h"
@interface ActivityDetailController ()<UITableViewDelegate,UITableViewDataSource>
{
     UITableView *_tableView;
    NSString *_themes;
    NSString *_campaign_introduce;
    NSString *_campaign_end_time;
    NSString *_campaign_start_time;
    NSString *_campaign_comment;

}
@property (nonatomic,strong)NSMutableArray *dataArray1;
@end

@implementation ActivityDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title  = @"活动详情";
    _dataArray1 = [NSMutableArray array];
    UIButton *backbutton = [[UIButton alloc]initWithFrame:CGRectMake(10, 20, 15, 25)];
    [backbutton setBackgroundImage:[UIImage imageNamed:@"香草园地-发布信息_03@2x"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(goBacktoUp) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:backbutton];
    self.navigationItem.leftBarButtonItem = backItem;
    [self questData];

}
-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_bg_@2x"]forBarMetrics:UIBarMetricsDefault];
}
-(void)goBacktoUp
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)questData{

    NSString *url = [NSString stringWithFormat:@"%@campaign/details?campaign_guid=%@",ServiceURL2,_campaign_guid];
    [YBRequestManager GET:url finished:^(NSData *data) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dic = dict[@"data"];
        _themes = dic[@"campaign_theme"];
        _campaign_introduce = dic[@"campaign_introduce"];
        _campaign_end_time = dic[@"campaign_end_time"];
        _campaign_start_time = dic[@"campaign_start_time"];
        _campaign_comment = dic[@"campaign_comment"];

        NSArray *array2 = dic[@"saleDetailInfoList"];


        NSArray *array3 = [ActivityDetailModel arrayOfModelsFromDictionaries:array2 error:nil];

        [_dataArray1 addObjectsFromArray:array3];
        [self cerateTableView];

        [_tableView reloadData];

        
    } failed:^(NSString *error) {
        
    }];
    
}
-(void)cerateTableView
{

    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UIScreen_WIDTH, UIScreen_HEIGHT-50)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    UIView *headview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UIScreen_WIDTH, 400*HEIGHT)];
    _tableView.tableHeaderView = headview;
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, UIScreen_WIDTH, 400*HEIGHT)];
    image.image = [UIImage imageNamed:@"活动背景"];
    [headview addSubview:image];
    UILabel *theme = [[UILabel alloc]initWithFrame:CGRectMake(UIScreen_WIDTH/2-100, 52*HEIGHT, 200, 20)];
    theme.text = _themes;
    theme.font = [UIFont systemFontOfSize:16];
    theme.textAlignment = NSTextAlignmentCenter;
    [image addSubview:theme];
    
    UILabel *introduce = [[UILabel alloc]initWithFrame:CGRectMake(UIScreen_WIDTH/2-100*HEIGHT, 150*HEIGHT, 200*HEIGHT, 100*HEIGHT)];
    introduce.text = _campaign_introduce;
    introduce.textColor = kRGBColor(38, 39, 38);
    introduce.font = [UIFont systemFontOfSize:16];
    [image addSubview:introduce];
    
    UILabel *start = [[UILabel alloc]initWithFrame:CGRectMake(UIScreen_WIDTH/2-100, 345*HEIGHT, 200, 20)];
    start.text = _campaign_start_time;
    start.textColor = kRGBColor(38, 39, 38);;
    start.textAlignment = NSTextAlignmentCenter;
    start.font = [UIFont systemFontOfSize:15];
    [image addSubview:start];
    UILabel *end = [[UILabel alloc]initWithFrame:CGRectMake(UIScreen_WIDTH/2-100, 365*HEIGHT, 200, 20)];
    end.text = _campaign_end_time;
    end.textAlignment = NSTextAlignmentCenter;
    end.textColor = kRGBColor(38, 39, 38);
    end.font = [UIFont systemFontOfSize:15];
    [image addSubview:end];
    
    UIView *footview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UIScreen_WIDTH, 120*HEIGHT)];
    footview.backgroundColor = kRGBColor(236, 237, 238);
    _tableView.tableFooterView = footview;
    UILabel *comment = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 70, 20)];
    comment.text = @"备注";
    [footview addSubview:comment];
    
    UILabel *comment2 = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, UIScreen_WIDTH-20, 60)];
    comment2.text = _campaign_comment;
    [footview addSubview:comment2];
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消选中
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *identifier = @"ActivityDetailCell";
    
    ActivityDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell ==nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"ActivityDetailCell" owner:nil options:nil][0];
    }
    ActivityDetailModel *model = _dataArray1[indexPath.row];
    [cell setModel:model];
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray1.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 210;
}

@end
